package com.authine.cloudpivot.engine.service.bizrule.handler;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.engine.component.query.api.FilterExpression;
import com.authine.cloudpivot.engine.component.query.api.FilterExpression.Op;
import com.authine.cloudpivot.engine.component.query.api.Order;
import com.authine.cloudpivot.engine.component.query.api.Pageable;
import com.authine.cloudpivot.engine.component.query.api.Sortable;
import com.authine.cloudpivot.engine.component.query.api.helper.OrderImpl;
import com.authine.cloudpivot.engine.component.query.api.helper.PageableImpl;
import com.authine.cloudpivot.engine.component.query.api.helper.Q;
import com.authine.cloudpivot.engine.component.query.api.helper.SortableImpl;
import com.authine.cloudpivot.engine.domain.runtime.BizObject;
import com.authine.cloudpivot.engine.enums.type.DefaultPropertyType;
import com.authine.cloudpivot.engine.open.domain.bizmodel.BizObjectQuery;
import com.authine.cloudpivot.engine.open.service.OpenEngine;
import com.authine.cloudpivot.engine.service.bizrule.handler.context.Request;
import com.google.common.collect.Lists;

public class 查询示例 extends BaseHandler {
    private static final Logger LOG = LoggerFactory.getLogger("在线编码");
    protected static final String SCHEMA_CODE = "model_wpxx";

    @Override
    public Object handle(Request request) {
        /* 获取系统开放引擎 */
        OpenEngine engine = this.getOpenEngine(request);
        /* 获取当前数据 */
        BizObject bizObject = request.getData().getBizObject();
        /* 查询目标数据 */
        List<BizObject> targets = engine.listBizObjects(new BizObjectQuery() {
            private static final long serialVersionUID = -6517590055934352108L;

            @Override
            public String getSchemaCode() {
                return SCHEMA_CODE;
            }

            @Override
            public FilterExpression getFilterExpression() {
                return Q.and(Q.it("wpgg", Op.Like, "米"), Q.it("wpdw", Op.Like, "台"));
            }

            @Override
            public Sortable getSortable() {
                return new SortableImpl(Lists.newArrayList(new OrderImpl(DefaultPropertyType.CREATED_TIME.getCode(), Order.Dir.DESC)));
            }

            @Override
            public Pageable getPageable() {
                return new PageableImpl(0, 2);
            }
        });
        LOG.info("===>>查询到的数据:{}", JSON.toJSONString(targets));
        return bizObject;
    }

}